<template>
	<view>
		<block v-if="dataBuffer">
			<view class="item" v-for="(res, index) in siteList" :key="res.id" @click="select(index)"> <u-icon
					name="close-circle-fill" :size="40" color="#ff5500" :index="res.id"
					@click.native.stop="delAddress(res.id,index)"></u-icon>
				<view class="top">
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag">
						<text class="red" v-if="res.status == 1">默认</text>
					</view>
				</view>
				<view class="bottom">
					{{res.location+res.useraddress}}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id"
						@click.native.stop="editAddres(index)"></u-icon>
				</view>
			</view>
		</block>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				siteList: [], //初始化地址数据
				dataBuffer: true
			};
		},
		onShow() {
			this.getAddres()
		},
		methods: {
			// 去编辑地址页面
			editAddres(index) {
				uni.setStorageSync('address', this.siteList[index])
				uni.navigateTo({
					url: '/pages/address/addSite?type=1'
				})
			},
			// 选择地址
			select(index) {
				let address = JSON.stringify(this.siteList[index])
				uni.navigateTo({
					url: '/pages/order/order?addr=' + address
				})
			},
			// 删除地址
			async delAddress(id, index) {
				let {
					token
				} = uni.getStorageSync('userInfo')
				let options = {
					url: '/api/addressremove',
					data: {
						id
					},
					header: {
						authorization: token
					},
					method: 'POST'
				}
				console.log(options);
				// console.log(1);
				// return
				let result = await this.$http(options)
				console.log(result);
				if (result.data.code == 200) {
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					})
					this.siteList.splice(index, 1)

				} else if (result.data.code == 403) {
					this.localBuffer = false
					uni.showToast({
						title: '请重新登录',
						icon: 'error'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 2000)
				} else {
					uni.showToast({
						title: '网络故障',
						icon: 'error'
					})
					this.localBuffer = false
				}
			},
			// 跳转新建收货地址
			toAddSite() {
				uni.navigateTo({
					url: '/pages/address/addSite'
				})
			},
			// 获取收货地址列表
			async getAddres() {
				let {
					uid,
					token
				} = uni.getStorageSync('userInfo')
				let options = {
					url: '/api/addresslist',
					data: {
						uid
					},
					header: {
						authorization: token
					}
				}
				let result = await this.$http(options)
				// console.log(result);
				if (result.data.code == 200) {
					if (result.data.list == null) { //请求成功但没有默认地址
						this.dataBuffer = false
						return
					}
					console.log(result);
					this.siteList = result.data.list
					this.dataBuffer = true
				} else if (result.data.code == 403) {
					this.dataBuffer = false
					uni.showToast({
						title: '请重新登录',
						icon: 'error'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 2000)
				} else {
					uni.showToast({
						title: '网络故障',
						icon: 'error'
					})
					this.dataBuffer = false
				}

			},
			// 跳转到新增地址页面
			toAddSite() {
				uni.navigateTo({
					url: './addSite'
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import '@/static/css/address.scss';
</style>